#include "netspeed.h"

#include <QTextStream>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopWidget>

netspeed::netspeed(QDialog *parent)
    : QDialog(parent), read_ok(false), DesktopSize(QApplication::desktop()->size())
{
    this->resize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->setWindowFlags(Qt::FramelessWindowHint  | Qt::SplashScreen | Qt::WindowStaysOnTopHint);

    /* Set App StyleSheet */
    set_app_stylesheet(":/qss/style.qss");

    /* Init */
    init_member();

    /* Connect SIGNAL SLOT */
    QObject::connect(m_net_frame.get(), SIGNAL(show_sysinfo_gui()), m_info_frame.get(), SLOT(show_win()));
    QObject::connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(read_sysinfo()));
    QObject::connect(m_info_frame.get(), SIGNAL(hide_change_mainwindow_size()), this, SLOT(hide_change_size()));
    QObject::connect(m_info_frame.get(), SIGNAL(show_change_mainwindow_size()), this, SLOT(show_change_size()));

    /* Start Timer */
    m_timer->start(1000);

    this->move(DesktopSize.width() - utils::NET_GUI_WIDTH ,
               DesktopSize.height() - utils::NET_GUI_HEIGHT - utils::PANEL_HEIGHT);
}

void netspeed::init_member()
{
    /* Set MainWindow Clear */
    this->setAttribute(Qt::WA_TranslucentBackground);
    /* New Net Frame */
    m_net_frame = boost::make_shared<Frame>(this);
    /* New Info Frame */
    m_info_frame = boost::make_shared<SysInfo>(this);
    /* New Layout */
    m_layout = boost::make_shared<QVBoxLayout>(this);
    m_layout->addWidget(m_info_frame.get());
    m_layout->addWidget(m_net_frame.get());
    m_layout->setMargin(0);
    m_layout->setSpacing(utils::LAYOUT_SPACE);
    this->setLayout(m_layout.get());
    /* New Timer */
    m_timer = boost::make_shared<QTimer>(this);
    /* First Time to Read Info */
    read_sysinfo();
}

bool netspeed::set_app_stylesheet(QString filename)
{
    /* Read and Open the StyleFile */
    QFile style_file(filename.toLocal8Bit().data());
    if(!style_file.open(QFile::ReadOnly)) {
        syslog(LOG_ERR, "Open Style file Failure, Please Check it!!!\n");
        return false;
    }

    /* Set The StyleSheet for App */
    QString style_sheet = QLatin1String(style_file.readAll());
    this->setStyleSheet(style_sheet);
    style_file.close();
    return true;
}

void netspeed::read_sysinfo()
{
    utils::Speed_percent speed_percent = m_net_frame->read_net();
    double cpu_percent = m_info_frame->read_cpu();
    double mem_percent = m_info_frame->read_mem();

    if(read_ok) {
        m_net_frame->update_net_gui(speed_percent);
        m_info_frame->update_info_gui(cpu_percent, mem_percent);
    } else {
        read_ok = true;
        /* We Try again */
        read_sysinfo();
    }
}

void netspeed::show_change_size()
{
    QPoint BasePoint = this->pos();
    this->resize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT + utils::LAYOUT_SPACE + utils::INFO_GUI_HEIGHT);
    this->setMaximumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT + utils::LAYOUT_SPACE + utils::INFO_GUI_HEIGHT);
    this->setMinimumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT + utils::LAYOUT_SPACE + utils::INFO_GUI_HEIGHT);
    this->move(BasePoint - QPoint(0, utils::INFO_GUI_HEIGHT + utils::LAYOUT_SPACE ));
}

void netspeed::hide_change_size()
{
    QPoint BasePoint = this->pos();
    this->resize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->setMaximumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->setMinimumSize(utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
    this->setGeometry(BasePoint.x(),BasePoint.y() + utils::INFO_GUI_HEIGHT + utils::LAYOUT_SPACE,
                      utils::NET_GUI_WIDTH, utils::NET_GUI_HEIGHT);
}

void netspeed::close_win()
{
    this->close();
}

void netspeed::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
        this->move(event->globalPos() - m_mouse_dest_pos);
}

void netspeed::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
        m_mouse_dest_pos = event->globalPos() - this->pos();
}

void netspeed::mouseReleaseEvent(QMouseEvent *)
{
    /* Out of Screen, We Skip it */
    while((this->x() + this->width()) > DesktopSize.width()) {
        usleep(1000);
        this->move(this->pos() + QPoint(-1, 0));
    }
    while((this->y() + this->height() ) > (DesktopSize.height() - utils::PANEL_HEIGHT )) {
        usleep(1000);
        this->move(this->pos() + QPoint(0, -1));
    }
}

void netspeed::paintEvent(QPaintEvent *e)
{
    /* There is a Bug, I do not Why, So, I must fixed it */

    /* Out of Screen, We Skip it */
    while((this->x() + this->width()) > DesktopSize.width()) {
        usleep(1000);
        this->move(this->pos() + QPoint(-1, 0));
    }
    while((this->y() + this->height() ) > (DesktopSize.height() - utils::PANEL_HEIGHT )) {
        usleep(1000);
        this->move(this->pos() + QPoint(0, -1));
    }

    QDialog::paintEvent(e);
}
